﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace yule.Common.Log
{
    internal sealed class FileLogWrite : ILogWrite
    {
        private static object log_lock = new object();

        private static void WriteToFile(string message, string fileName)
        {
            if (System.IO.File.Exists(fileName) == false)
            {
                using (FileStream fs = System.IO.File.Create(fileName))
                {
                    message += "\r\n";
                    byte[] bs = System.Text.Encoding.UTF8.GetBytes(message);
                    fs.Write(bs, 0, bs.Length);
                    fs.Close();
                }
            }
            else
            {
                using (StreamWriter sw = File.AppendText(fileName))
                {
                    sw.WriteLine(message);
                    sw.Close();
                }
            }
        }

         #region ILogWrite 成员

        public void Write(string message, Level level)
        {
            string fileName = System.AppDomain.CurrentDomain.BaseDirectory + "\\yule_log.txt";
            lock (log_lock)
            {
                WriteToFile(message, fileName);
            }
        }

        #endregion
    }
}
